$(document).ready(function(){
	//Generate list of month
	for (var i = 1; i <= 12; i++) {
		var month = $('<option value="' + i + '">Thang ' + i + '</option>');
		$('#thang').append(month);
	}
	//Generate list of year
	for (var i = 0; i < 8; i++) {
		var year = $('<option value="' + (i + 2013) + '">' + (i + 2013) + '</option>');
		$('#nam').append(year);
	}
	//Get info about today
	var currentDay = new Date();
	var day = currentDay.getDate();
	var month = currentDay.getMonth() + 1;
	var year = currentDay.getFullYear();
	var url = constructDayUrl(day, month, year);
	fetchInfo(url);
	$('#datebox').bind('datebox', function(e, passed){
		if (passed.method === "set") {
			$('#datebox').datebox('option', {
				calHighToday: false
			});
			$('#simple-result').html('');
			var splited = passed.value.split('/');
			day = splited[0];
			month = splited[1];
			year = splited[2];
			url = constructDayUrl(day, month, year);
			fetchInfo(url);
			$('#result').popup('open', {
				x: 0,
				y: 342,
				transition: "slideup",
				positionTo: "origin"
			});
		}
	});
	$('#xem-ket-qua').click(function(){
		$('#simple-result').html('');
		var thang = $('#thang').val();
		var nam = $('#nam').val();
		var suKienId = $('#sukien').val();
		var url = "http://api.tamlinh.vn/lichvansu/chonngaytot/danhsachngaysukien/thangxem/"
				+ thang + "/namxem/" + nam + "/key/tvi1102/sukienid/" + suKienId;
		var dateArray = [];
		$.ajax({
			url: url,
			dataType: "xml",
			error: function(e, txt, txt1) {
				alert(txt + "; " + txt1);
			},
			success: function(data) {
				$(data).find('item').each(function(){
					var time = $(this).text().split(' ');
					var date = time[1];
					dateArray.push(date);
				});
				$('#datebox').datebox("option", {
					highDatesAlt: dateArray,
				});
				var displayDate = new Date();
				displayDate.setFullYear(nam, thang - 1, 1);
				$('#datebox').datebox('setTheDate', displayDate);
			}
		});
	});
	//Construct an url for information fetching
	function constructDayUrl(day, month, year) {
		return "http://api.tamlinh.vn/lichVanSu/xemNgay/xem/ngay/" + day + "/thang/" 
		+ month + "/nam/" + year + "/key/tvi1102";
	}
	function fetchInfo(url) {
		$.ajax({
			url: url,
			dataType: "xml",
			error: function(e, txt, txt1) {
				alert(txt + "; " + txt1);
			},
			success: function(data) {
				$(data).find('item').each(function(){
					var name = $(this).find('name').text();
					var value = $(this).find('value').text();
					switch (name) {
					case "giohoangdao": {
						var pElem = $("<p>" + value + "</p>");	
						$('#simple-result').prepend(pElem);
						var bElem = $("<p><b>Giờ tốt</b></p>");
						$('#simple-result').prepend(bElem);
						break;
					}
					case "totdoivoi": {
						if (value != "") {
							var bElem = $("<p><b>Nên làm</b></p>");
							$('#simple-result').append(bElem);
							$('#simple-result').append("<ul id='nenlam'></ul>");
							var substr = value.split('/ ');
							for (var i = 0; i < substr.length; i++) {
								$('#nenlam').append("<li>" + substr[i] + "</li>");
							}
						}
						break;
					}
					case "khongnenlam": {
						if (value != "") {
							var bElem = $("<p><b>Không nên làm</b></p>");
							$('#simple-result').append(bElem);
							$('#simple-result').append("<ul id='khongnenlam'></ul>");
							var substr = value.split('/ ');
							for (var i = 0; i < substr.length; i++) {
								$('#khongnenlam').append("<li>" + substr[i] + "</li>");
							}
						}
						break;
					}
					default:
						break;
					}
				});
			}
		});
	}
	
	// Doi ngay am/duong
	$('#doingay_btnSubmit').click(function() {
		var strMode = 'amSangDuong';
		if ($('input[name=doingay_selMode]:checked').val() == 1) {
			strMode = 'duongSangAm';
		}
		var date = $('#doingay_flipbox').datebox('getTheDate');		
		var strUrl = 'http://api.tamlinh.vn/lichVanSu/doiNgay/' + strMode + '/ngay/' + date.getDate() + 
			'/thang/' + (date.getMonth() + 1) + '/nam/'  + date.getFullYear() +  '/key/tvi1102';
		var strContent = '';
		
		$.ajax({
			url: strUrl,
			dataType: "xml",
			error: function(e, txt, txt1) {
				alert(txt + "; " + txt1);
			},
			success: function(data) {
				$(data).find('item').each(function(){
					var title = $(this).find('title').text();
					var value = $(this).find('value').text();
					
					strContent += '<strong>' + title + '</strong> ' + value + '<br />';
				});
			},
			complete: function() {
				$('#doingay_result').html(strContent);
			}
		});
	});
});
